﻿using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace EyFramework.Library
{
    public partial class GridToolBar : UserControl
    {
        //定义委托
        public delegate void ToolBarClickHandle(object sender, ItemClickEventArgs e);

        //定义事件
        public event ToolBarClickHandle ToolBarClick;

        public List<GridToolBarSetting> bars = new List<GridToolBarSetting>();

        public GridToolBar()
        {
            InitializeComponent();
            InitDefaultBars();
        }

        private void InitDefaultBars()
        {
            bars.Add(new GridToolBarSetting(bars.Count + 1, "新增(&A)", 0));
            bars.Add(new GridToolBarSetting(bars.Count + 1, "修改(&U)", 1));
            bars.Add(new GridToolBarSetting(bars.Count + 1, "删除(&D)", 2));
        }

        /// <summary>
        /// 初始化工具条
        /// </summary>
        public void InitBars()
        {
            foreach (var barSetting in bars)
            {
                var bar = new BarButtonItem(barManager1, barSetting.Caption) { Tag = barSetting, Name = barSetting.Name, Id = barSetting.Id, ImageIndex = (barSetting.ImageIndex > -1 ? barSetting.ImageIndex : -1) };
                barManager1.Items.Add(bar);
                toolBar.LinksPersistInfo.Insert(toolBar.LinksPersistInfo.Count,
                    new LinkPersistInfo(DevExpress.XtraBars.BarLinkUserDefines.PaintStyle, bar, "", true, true, true, 0, null, DevExpress.XtraBars.BarItemPaintStyle.CaptionGlyph));
                bar.ItemClick += barItem_ItemClick;
            }
        }

        private void barItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (ToolBarClick != null)
                ToolBarClick(sender, e);
        }

        private void GridToolBar_Load(object sender, EventArgs e)
        {
            InitBars();
        }
    }
}